//
//  CommissionRunController.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 2017/3/17.
//  Copyright © 2017年 EasyToTesco. All rights reserved.
//

#import "CommissionRunController.h"
#import "TopStatusView.h"
#import "RechargeCell.h"
#import "CommissionRunModel.h"

@interface CommissionRunController ()<UITableViewDelegate,UITableViewDataSource>
{
    MBProgressHUD *progressHUD;
    NSUInteger _refreshIndex;
    UIImageView *noDrawImage;
}

@property(nonatomic,strong)UITableView *commissiomRunView;

@property(nonatomic,strong)NSMutableArray *commissiomRunArray;

@property(nonatomic,strong)TopStatusView *topStatusView;

@property(nonatomic,strong)UILabel  *current_commissionLabel;

@end

@implementation CommissionRunController

static NSString *const commissiomRunIdentifier = @"commissiomRunIdentifier";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    _refreshIndex = 1;
    [self commissiomRunListData:_refreshIndex];
    [self buildView];
}

#pragma mark >>>>>>>>刷新
- (void)commissiomRunListLoadMoreData
{
    _refreshIndex ++;
    [self commissiomRunListData:_refreshIndex];
    [_commissiomRunView.mj_footer endRefreshing];
}


- (void)commissiomRunListData:(NSInteger)refreshIndex
{
    progressHUD = [MBProgressHUD loadingInformationToView];
    [self.view addSubview:progressHUD];
    
    if (refreshIndex == 1) {
        [self.commissiomRunArray removeAllObjects];
    }
    
    NSString *refreshIndexStr = [NSString stringWithFormat:@"%zd",refreshIndex];
    
    NSDictionary *params = @{@"username":USERNAME,@"page":refreshIndexStr};
    NSLog(@"%@",params);
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"/api/Expand/commissionStreamHistory" completeBlock:^(id object) {
        
        NSLog(@"%@",object);
        if (JudgeBackDataCode) {
            
            NSDictionary *dataDict = [object objectForKey:@"data"];
            
            //剩余佣金
            if ([dataDict objectForKey:@"current_commission"]) {
                self.current_commissionLabel.text = [NSString stringWithFormat:@"剩余佣金 : %@",[dataDict objectForKey:@"current_commission"]];
            }
            
            NSArray *listArray = [dataDict objectForKey:@"list"];
            for (NSDictionary *dict in listArray) {
                CommissionRunModel *commissionRunModel = [[CommissionRunModel alloc] initWithDictionary:dict error:nil];
                
                if (commissionRunModel) {
                    [self.commissiomRunArray addObject:commissionRunModel];
                }
            }
            
        }else{
            
            //订单没有
            if (refreshIndex == 1) {
                
                noDrawImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_NoRecord"]];
                [self.view addSubview:noDrawImage];
                
                [noDrawImage mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.equalTo(self.view.mas_centerX);
                    make.centerY.equalTo(self.view.mas_centerY).offset(-HEIGHT/8);
                    make.width.mas_equalTo(WIDTH/1.5);
                    make.height.mas_equalTo(WIDTH/2.4);
                }];
                
                [self.commissiomRunView removeFromSuperview];
                
            }else{
                
                //没有更多数据
                [_commissiomRunView.mj_footer endRefreshingWithNoMoreData];
                
            }
            
            
        }
        [progressHUD hide:YES];
        [_commissiomRunView reloadData];
        
        
    }];
    
}



#pragma mark 数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.commissiomRunArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    RechargeCell *cell=[tableView dequeueReusableCellWithIdentifier:commissiomRunIdentifier];
    
    if (cell==nil) {
        cell=[[RechargeCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:commissiomRunIdentifier];
    }
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    
    CommissionRunModel *commissionRunModel = self.commissiomRunArray[indexPath.row];
    
    //变化佣金
    cell.UserLabel.text = commissionRunModel.operator_commission;
    //剩余佣金
    cell.moneyLabel.text = commissionRunModel.current_commission;
    //状态
    if ([commissionRunModel.type isEqualToString:@"1"]) {
        cell.timeLabel.text = @"增加";
    }else if ([commissionRunModel.type isEqualToString:@"2"]){
        cell.timeLabel.text = @"减少";
    }else{
        cell.timeLabel.text = @"增加";
    }
    
    //时间
    cell.statusLabel.text = commissionRunModel.create_time;
    

    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}


- (void)buildView
{
    self.title = @"佣金流水记录";
    self.view.backgroundColor = BACKGROUND_COLOR;
    TopStatusView *topStatusView = [[TopStatusView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 50) titleArray:@[@"变化佣金",@"剩余佣金",@"操作状态",@"时间"]];
    [self.view addSubview:topStatusView];
    self.topStatusView = topStatusView;
    
//
    self.commissiomRunView = [[UITableView alloc] initWithFrame:CGRectMake(0,50,WIDTH, HEIGHT -64-50 - 50) style:UITableViewStylePlain];
    [self.view addSubview:_commissiomRunView];
    _commissiomRunView.delegate = self;
    _commissiomRunView.dataSource = self;
    _commissiomRunView.backgroundColor = BACKGROUND_COLOR;
    _commissiomRunView.rowHeight = 45;
    [_commissiomRunView registerNib:[UINib nibWithNibName:@"RechargeCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:commissiomRunIdentifier];
    
    self.commissiomRunView.mj_footer = [TopPullRefreshAnaimation footerWithRefreshingTarget:self refreshingAction:@selector(commissiomRunListLoadMoreData)];
    
    self.current_commissionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,HEIGHT-50-64, WIDTH,50)];
    [self.view addSubview:self.current_commissionLabel];
    _current_commissionLabel.textColor = DEFAULT_COLOR;
    _current_commissionLabel.backgroundColor = [UIColor whiteColor];
    _current_commissionLabel.font = BOLDFONT(15);
    _current_commissionLabel.text = @"";
    _current_commissionLabel.textAlignment = NSTextAlignmentCenter;
    _current_commissionLabel.layer.borderColor = [UIColor grayColor].CGColor;
    _current_commissionLabel.layer.borderWidth = 0.2f;
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (NSMutableArray *)commissiomRunArray
{
    if (!_commissiomRunArray) {
        _commissiomRunArray = [NSMutableArray array];
    }
    return _commissiomRunArray;
}

@end
